package cn.fl.warrant.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cmm.comm.facade.framework.api.ISysDictionaryApiService;
import cmm.comm.facade.framework.dto.SysDictionaryDTO;
import cmm.mid.core.framework.page.KeyValueVO;
import cmm.mid.core.framework.page.Pager;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.warrant.service.IArcReceiptMainService;
import cn.fl.warrant.transform.ArcReceiptMainVOTransform;
import cn.fl.warrant.vo.ArcReceiptMainVO;
import fl.warrant.facade.archive.api.IArcReceiptMainApiService;
import fl.warrant.facade.archive.condition.ArcReceiptConditionRQueryCondition;
import fl.warrant.facade.archive.condition.ArcReceiptDetailQueryCondition;
import fl.warrant.facade.archive.condition.ArcReceiptMainQueryCondition;
import fl.warrant.facade.archive.dto.ArcReceiptConditionRDTO;
import fl.warrant.facade.archive.dto.ArcReceiptDetailDTO;
import fl.warrant.facade.archive.dto.ArcReceiptMainDTO;

/**
 * @Description: 交接单主表 客户端服务层接口实现
 * @Description: 业务层方法名称前缀：新增：add* 删除：remove* 修改：modify* 查询对象：select* get* 批量：batch* 查询集合：search*
 */
@Service
public class ArcReceiptMainServiceImpl implements IArcReceiptMainService {
	
	private static final Logger logger = LoggerFactory.getLogger(IArcReceiptMainService.class);
	
	/**
	 * 数据字典
	 */
	@Autowired
	private ISysDictionaryApiService sysDictionaryApi;
	
	@Autowired
	private IArcReceiptMainApiService arcReceiptMainApiService;
	
	
	/**
	 * 根据字段代码组获取代码值集合
	 * @param grpCd 代码组值
	 * @param sysCd 系统代码
	 * @param orgCd 组织结构代码
	 * @return 值
	 * @throws BaseException
	 */
	@Override
	public List<KeyValueVO> getKvList(String grpCd) throws BaseException {
		List<KeyValueVO>  keyValueList = null;
		List<SysDictionaryDTO> dictionaryList = this.sysDictionaryApi
				.searchGroup(CurrentThreadContext.getCurrentSysCd(),
						CurrentThreadContext.getCurrentOrgCd(), grpCd);
		if(CollectionUtils.isNotEmpty(dictionaryList)){
			keyValueList = new ArrayList<>();
			for (SysDictionaryDTO sysDictionaryDTO : dictionaryList) {
				KeyValueVO keyValue = new KeyValueVO();
				keyValue.setKey(sysDictionaryDTO.getCode());
				keyValue.setValue(sysDictionaryDTO.getCodeNm());
				keyValueList.add(keyValue);
			}
		}
		return keyValueList;
	}
	
	/**
	 * 根据字段代码组获取代码值集合
	 * @param grpCd 代码组值
	 * @param sysCd 系统代码
	 * @param orgCd 组织结构代码
	 * @return 值
	 * @throws BaseException
	 */
	@Override
	public List<KeyValueVO> getKvDescList(String grpCd) throws BaseException {
		List<KeyValueVO>  keyValueList = null;
		List<SysDictionaryDTO> dictionaryList = this.sysDictionaryApi
				.searchGroup(CurrentThreadContext.getCurrentSysCd(),
						CurrentThreadContext.getCurrentOrgCd(), grpCd);
		if(CollectionUtils.isNotEmpty(dictionaryList)){
			keyValueList = new ArrayList<>();
			for (SysDictionaryDTO sysDictionaryDTO : dictionaryList) {
				KeyValueVO keyValue = new KeyValueVO();
				keyValue.setKey(sysDictionaryDTO.getCodeNm());
				keyValue.setValue(sysDictionaryDTO.getCodeDesc());
				keyValueList.add(keyValue);
			}
		}
		return keyValueList;
	}
	
	@Override
	public ArcReceiptMainVO selectById(Long id) throws BaseException{
		return ArcReceiptMainVOTransform.toVO(arcReceiptMainApiService.selectById(id));
	}

	@Override
	public Long add(ArcReceiptMainVO entity) throws BaseException{
		return this.arcReceiptMainApiService.add(ArcReceiptMainVOTransform.toDTO(entity));
	}

	@Override
	public boolean removeById(Long id) throws BaseException{
		return this.arcReceiptMainApiService.removeById(id);
	}
	


	@Override
	public boolean update(ArcReceiptMainVO entity) throws BaseException{
		return this.arcReceiptMainApiService.update(ArcReceiptMainVOTransform.toDTO(entity));
	}

	@Override
	public Pager searchListPage(Pager page) throws BaseException{
		return this.arcReceiptMainApiService.searchListPage(page);
	}
	/**
	 * 查询交接单筛选条件
	 */
	@Override
	public List<ArcReceiptConditionRDTO> searchArcReceiptConditionRList(
			ArcReceiptConditionRQueryCondition condition) throws BaseException {
		return arcReceiptMainApiService.searchArcReceiptConditionRList(condition);
	}
	
	public List<ArcReceiptMainDTO> searchList(ArcReceiptMainQueryCondition condition) throws BaseException {
		return arcReceiptMainApiService.searchList(condition);
	}
	
	@Override
	public boolean updateArcReceiptMain(ArcReceiptMainVO entity) throws BaseException{
		return this.arcReceiptMainApiService.updateArcReceiptMain(ArcReceiptMainVOTransform.toDTO(entity));
	}
	
	public int updateCurrentVersion(ArcReceiptMainVO vo) throws BaseException{
		return arcReceiptMainApiService.updateCurrentVersion(ArcReceiptMainVOTransform.toDTO(vo));
	}

	@Override
	public List<ArcReceiptDetailDTO> arcReceiptDetailList(
			ArcReceiptDetailQueryCondition qc) throws BaseException {
		return arcReceiptMainApiService.arcReceiptDetailList(qc);
	}
	@Override
	public List<ArcReceiptMainVO> selectListForMgr(
			ArcReceiptMainQueryCondition condition) throws BaseException {
		return ArcReceiptMainVOTransform.toVOList(this.arcReceiptMainApiService.selectListForMgr(condition));
	}

	@Override
	public Long getMaxVersion(String receiptNo) throws BaseException {
		
		Long defaultReturn = 1L;
		
		if(StringUtils.isNotBlank(receiptNo)) {
			ArcReceiptMainQueryCondition qc = new ArcReceiptMainQueryCondition();
			qc.setReceiptNo(receiptNo);
			List<ArcReceiptMainDTO> arcReceiptMainDTOList = arcReceiptMainApiService.searchList(qc);
			if(arcReceiptMainDTOList != null && arcReceiptMainDTOList.size() >0) {
				for (ArcReceiptMainDTO arcReceiptMainDTO : arcReceiptMainDTOList) {
					if(arcReceiptMainDTO.getVersion() != null && arcReceiptMainDTO.getVersion().compareTo(defaultReturn) > 0) {
						defaultReturn = arcReceiptMainDTO.getVersion();
					}
				}
			}
		}
		
		return defaultReturn;
	}

	@Override
	public List<ArcReceiptMainVO> checkArcReceiptR(
			ArcReceiptMainQueryCondition qc) throws BaseException {
		return ArcReceiptMainVOTransform.toVOList(arcReceiptMainApiService.checkArcReceiptR(qc));
	}
	
}
